﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using GCTrackableRace.Interfaces;
//using System.Data;
//using System.Data.SQLite;
//using GcTrackableRace.Entities;

//namespace GCTrackableRace.Persistence.Gsak
//{
//    public class Sqlite : IPersistence
//    {
//        private readonly string _TrackableDbLocation;
//        private readonly string _ConnectionString;

//        public Sqlite(string trackableDbLocation)
//        {
//            _TrackableDbLocation = trackableDbLocation;
//            _ConnectionString = "data source=\"" + _TrackableDbLocation + "\"";
//        }

//        public IList<ITrackable> GetAllTrackables()
//        {
//            const string sqlStatement =
//                "Select Code, Name, PlacedBy, Country, State, County, UserData, User3, Symbol from Caches order by Name ASC";

//            var trackables = new List<ITrackable>();

//            using (var conn = new SQLiteConnection(_ConnectionString))
//            {
//                var cmd = new SQLiteCommand(sqlStatement, conn);

//                SQLiteDataReader reader = null;

//                conn.Open();

//                try
//                {
//                    reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

//                    while (reader.Read())
//                    {
//                        trackables.Add(
//                                       new Trackable
//                                        {
//                                            Code = reader["Code"].ToString(),
//                                            Name = reader["Name"].ToString(),
//                                            Owner = reader["PlacedBy"].ToString(),
//                                            Country = reader["Country"].ToString(),
//                                            State = reader["State"].ToString(),
//                                            County = reader["County"].ToString(),
//                                            TrackableType = reader["UserData"].ToString(),
//                                            TrackableParentName = reader["User3"].ToString()
//                                        }
//                            );
//                    }
//                }
//                finally
//                {
//                    if (reader != null)
//                        reader.Close();
//                }
//            }

//            return trackables;
//        }

//        public IList<ITrackableLog> GetAllTrackableLogs(string trackableCode)
//        {
//            const string sqlStatement =
//                "Select lLogId, lBy, lType, lDate, lLat, lLon, lParent as TrackableCode from Logs where lParent=@trackableCode order by lDate ASC, lLogId ASC;";

//            var trackableLogs = new List<ITrackableLog>();

//            using (var conn = new SQLiteConnection(_ConnectionString))
//            {
//                var cmd = new SQLiteCommand(sqlStatement, conn);

//                cmd.Parameters.Add(new SQLiteParameter("@trackableCode", trackableCode));

//                SQLiteDataReader reader = null;

//                conn.Open();

//                try
//                {
//                    reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

//                    PopulateItemLogFromReader(trackableLogs, reader);
//                }
//                finally
//                {
//                    if (reader != null)
//                        reader.Close();
//                }
//            }

//            return trackableLogs;
//        }

//        private void PopulateItemLogFromReader(List<ITrackableLog> logs, SQLiteDataReader reader)
//        {
//            while (reader.Read())
//            {
//                var lBy = reader["lBy"].ToString().Split(new[] {"$~"}, 2, StringSplitOptions.None);

//                if (lBy.Length == 1)
//                {
//                    Array.Resize(ref lBy, 2);
//                    lBy[1] = lBy[0];
//                    lBy[0] = "<no cache>";
//                }

//                logs.Add(
//                         new TrackableLog
//                            {
//                                LogId = int.Parse(reader["lLogId"].ToString()),
//                                TrackableCode = reader["TrackableCode"].ToString(),
//                                LogType = reader["lType"].ToString(),
//                                LocationGcCode = lBy[0],
//                                ByCacher = lBy[1],
//                                Date = DateTime.Parse(reader["lDate"].ToString()),
//                                Latitude = String.IsNullOrEmpty(reader["lLat"].ToString()) ? 0 : Double.Parse(reader["lLat"].ToString()),
//                                Longitude = String.IsNullOrEmpty(reader["lLon"].ToString()) ? 0 : Double.Parse(reader["lLon"].ToString()),
//                            }
//                    );
//            }
//        }
//    }
//}
